import 'package:flutter/material.dart';
import 'package:pcms/widgets/status/device_info_widget.dart';
import 'package:pcms/widgets/status/air_conditioner_status_widget.dart';
import 'package:pcms/widgets/status/air_source_unit_status_widget.dart';
import 'package:pcms/widgets/alarm/alarm_data_widget.dart';
import 'package:pcms/widgets/production/production_data_widget.dart';
import 'package:pcms/widgets/maintenance/maintenance_data_widget.dart';
import 'package:pcms/widgets/common/auto_scroll_widget.dart';

class DashboardScreen extends StatelessWidget {
  const DashboardScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: const Size.fromHeight(100),
        child: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [
                const Color(0xFF0A79DF).withOpacity(0.8),
                const Color(0xFF030A24),
              ],
            ),
            boxShadow: [
              BoxShadow(
                color: const Color(0xFF0A79DF).withOpacity(0.3),
                blurRadius: 10,
                offset: const Offset(0, 2),
              ),
            ],
          ),
          child: AppBar(
            backgroundColor: Colors.transparent,
            elevation: 0,
            centerTitle: true,
            title: Container(
              padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
              // 移除边框和渐变装饰
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  // 标题上方间距 - 调低标题位置
                  const SizedBox(height: 85),
                  // 标题文本
                  const Text(
                    '塞尚工厂生产监控',
                    style: TextStyle(
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                      letterSpacing: 2,
                    ),
                  ),
                  const SizedBox(height: 8), // 调高动态SVG分隔线位置
                  // 动态SVG分隔线
                  SizedBox(
                    width: 640,
                    height: 60,
                    child: CustomPaint(
                      painter: AnimatedLinePainter(),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
      body: Padding(
        padding: const EdgeInsets.all(6.0),
        child: Column(
          children: [
            // 第一行：运行状态和报警数据（增加高度比例）
            Expanded(
              flex: 5, // 从3增加到5，增加2行高度
              child: Row(
                children: [
                  // 运行状态板块
                  Expanded(
                    child: Container(
                      decoration: BoxDecoration(
                        color: const Color(0xFF0B1A3E),
                        borderRadius: BorderRadius.circular(8),
                        border: Border.all(
                          color: const Color(0xFF0A79DF).withOpacity(0.3),
                          width: 1,
                        ),
                        boxShadow: [
                          BoxShadow(
                            color: const Color(0xFF0A79DF).withOpacity(0.1),
                            blurRadius: 10,
                            spreadRadius: 1,
                          ),
                        ],
                      ),
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(8),
                        child: Container(
                          decoration: BoxDecoration(
                            gradient: LinearGradient(
                              begin: Alignment.topLeft,
                              end: Alignment.bottomRight,
                              colors: [
                                const Color(0xFF0A79DF).withOpacity(0.1),
                                Colors.transparent,
                              ],
                            ),
                          ),
                          child: Padding(
                            padding: const EdgeInsets.all(6.0),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                _buildSectionTitle('运行状态'),
                                const SizedBox(height: 8),
                                Expanded(
                                  child: SingleChildScrollView(
                                    child: Column(
                                      children: [
                                        // 方舱运行状态
                                        Container(
                                          width: double.infinity,
                                          margin: const EdgeInsets.only(bottom: 12),
                                          padding: const EdgeInsets.all(8),
                                          decoration: BoxDecoration(
                                            color: const Color(0xFF0F2A5A).withOpacity(0.6),
                                            borderRadius: BorderRadius.circular(6),
                                            border: Border.all(
                                              color: const Color(0xFF0A79DF).withOpacity(0.2),
                                              width: 1,
                                            ),
                                          ),
                                          child: Column(
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            children: [
                                              _buildSectionTitle('方舱运行状态'),
                                              const SizedBox(height: 8),
                                              const DeviceInfoWidget(),
                                            ],
                                          ),
                                        ),
                                        // XF-01和XF-02空调机组
                                        Row(
                                          children: [
                                            Expanded(
                                              child: Container(
                                                margin: const EdgeInsets.only(right: 6, bottom: 12),
                                                padding: const EdgeInsets.all(8),
                                                decoration: BoxDecoration(
                                                  color: const Color(0xFF0F2A5A).withOpacity(0.6),
                                                  borderRadius: BorderRadius.circular(6),
                                                  border: Border.all(
                                                    color: const Color(0xFF0A79DF).withOpacity(0.2),
                                                    width: 1,
                                                  ),
                                                ),
                                                child: const AirConditionerStatusWidget(title: 'XF-01空调机组信息'),
                                              ),
                                            ),
                                            Expanded(
                                              child: Container(
                                                margin: const EdgeInsets.only(left: 6, bottom: 12),
                                                padding: const EdgeInsets.all(8),
                                                decoration: BoxDecoration(
                                                  color: const Color(0xFF0F2A5A).withOpacity(0.6),
                                                  borderRadius: BorderRadius.circular(6),
                                                  border: Border.all(
                                                    color: const Color(0xFF0A79DF).withOpacity(0.2),
                                                    width: 1,
                                                  ),
                                                ),
                                                child: const AirConditionerStatusWidget(title: 'XF-02空调机组信息'),
                                              ),
                                            ),
                                          ],
                                        ),
                                        // 空气源模块机组
                                        Container(
                                          width: double.infinity,
                                          padding: const EdgeInsets.all(8),
                                          decoration: BoxDecoration(
                                            color: const Color(0xFF0F2A5A).withOpacity(0.6),
                                            borderRadius: BorderRadius.circular(6),
                                            border: Border.all(
                                              color: const Color(0xFF0A79DF).withOpacity(0.2),
                                              width: 1,
                                            ),
                                          ),
                                          child: Column(
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            children: [
                                              _buildSectionTitle('空气源模块机组信息'),
                                              const SizedBox(height: 8),
                                              const AirSourceUnitStatusWidget(),
                                            ],
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(width: 16),
                  // 报警数据板块
                  Expanded(
                    child: Container(
                      decoration: BoxDecoration(
                        color: const Color(0xFF0B1A3E),
                        borderRadius: BorderRadius.circular(8),
                        border: Border.all(
                          color: const Color(0xFF0A79DF).withOpacity(0.3),
                          width: 1,
                        ),
                        boxShadow: [
                          BoxShadow(
                            color: const Color(0xFF0A79DF).withOpacity(0.1),
                            blurRadius: 10,
                            spreadRadius: 1,
                          ),
                        ],
                      ),
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(8),
                        child: Container(
                          decoration: BoxDecoration(
                            gradient: LinearGradient(
                              begin: Alignment.topLeft,
                              end: Alignment.bottomRight,
                              colors: [
                                const Color(0xFF0A79DF).withOpacity(0.1),
                                Colors.transparent,
                              ],
                            ),
                          ),
                          child: Padding(
                            padding: const EdgeInsets.all(6.0),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                _buildSectionTitle('报警数据'),
                                const SizedBox(height: 8),
                                const Expanded(child: AlarmDataWidget()),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            const SizedBox(height: 16),
            // 第二行：生产数据和维护数据
            Expanded(
              flex: 2, // 保持为2
              child: Row(
                children: [
                  Expanded(child: _buildDashboardCard('生产数据', const ProductionDataWidget())),
                  const SizedBox(width: 16),
                  Expanded(child: _buildDashboardCard('维护数据', const MaintenanceDataWidget())),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildDashboardCard(String title, Widget child) {
    return Container(
      decoration: BoxDecoration(
        color: const Color(0xFF0B1A3E),
        borderRadius: BorderRadius.circular(8),
        border: Border.all(
          color: const Color(0xFF0A79DF).withOpacity(0.3),
          width: 1,
        ),
        boxShadow: [
          BoxShadow(
            color: const Color(0xFF0A79DF).withOpacity(0.1),
            blurRadius: 10,
            spreadRadius: 1,
          ),
        ],
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(8),
        child: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
              colors: [
                const Color(0xFF0A79DF).withOpacity(0.1),
                Colors.transparent,
              ],
            ),
          ),
          child: Padding(
            padding: const EdgeInsets.all(6.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                _buildSectionTitle(title),
                const SizedBox(height: 8),
                Expanded(child: AutoScrollWidget(child: child)),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildSectionTitle(String title) {
    IconData iconData;
    switch (title) {
      case '运行状态':
        iconData = Icons.settings;
        break;
      case '报警数据':
        iconData = Icons.warning;
        break;
      case '生产数据':
        iconData = Icons.analytics;
        break;
      case '维护数据':
        iconData = Icons.build;
        break;
      case '方舱运行状态':
        iconData = Icons.home_work;
        break;
      case '空气源模块机组信息':
        iconData = Icons.air;
        break;
      default:
        iconData = Icons.info;
    }
    
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 6.0),
      decoration: const BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: Color(0xFF0A79DF),
            width: 1,
          ),
        ),
      ),
      child: Row(
        children: [
          Icon(
            iconData,
            color: const Color(0xFF0A79DF),
            size: 16,
          ),
          const SizedBox(width: 8),
          Container(
            width: 2,
            height: 14,
            decoration: BoxDecoration(
              color: const Color(0xFF0A79DF),
              borderRadius: BorderRadius.circular(1),
            ),
            margin: const EdgeInsets.only(right: 8),
          ),
          Text(
            title,
            style: const TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.w600,
              color: Colors.white,
              letterSpacing: 0.5,
            ),
          ),
        ],
      ),
    );
  }
}

class AnimatedLinePainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = const Color(0xFF3f96a5)
      ..strokeWidth = 3
      ..style = PaintingStyle.stroke;
    
    final paint2 = Paint()
      ..color = const Color(0xFF3f96a5)
      ..strokeWidth = 2
      ..style = PaintingStyle.stroke;

    // 主要折线路径
    final path1 = Path()
      ..moveTo(0, 12)
      ..lineTo(115.2, 12)
      ..lineTo(128, 24)
      ..lineTo(160, 24)
      ..lineTo(172.8, 36)
      ..lineTo(460.8, 36)
      ..lineTo(480, 24)
      ..lineTo(512, 24)
      ..lineTo(524.8, 12)
      ..lineTo(640, 12);
    
    // 底部直线
    final path2 = Path()
      ..moveTo(192, 48)
      ..lineTo(448, 48);
    
    canvas.drawPath(path1, paint);
    canvas.drawPath(path2, paint2);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}